package cn.tedu.v5;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Version 4 客户端 【 登录 】 退出：退2次
 */
public class Client {
    private static final String HOST = "192.168.194.117";
    private static final Integer PORT = 7777;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;

    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch(SocketException e){
            System.out.println("链接被重置或断开");
        }catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开连接");
        }
    }

    public void start() {
        System.out.println("~~~~欢迎使用**聊天室~~~~");
        //目的：不捕获，确定没有问题，捕获代码块具体针对服务器结构
        try {
            //一、接收登录提示
            //1.读入服务器端--输出流
            String serverMessage=br.readLine();//服务器端：写“请输入用户名”
            System.out.println(serverMessage);//读入账号，必须写出
            //2.写出用户名

            Scanner scanner=new Scanner(System.in);
            String username=scanner.nextLine();
            pw.println(username);
            System.out.println("请输入密码");
            String serverMessage1= br.readLine();
            System.out.println(serverMessage1);
            String password=scanner.nextLine();
            pw.println(password);

            //接收登录结果
            serverMessage=br.readLine();

            //判断是否登录成功
            if(!equals(serverMessage)){
                chat(scanner);
            }else{
                System.out.println("与服务器的链接出现问题");
            }
        } catch (IOException e) {
            System.out.println("客户端断开连接");
        }
        try {
        } finally {
            if (pw != null)
                pw.close();
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败~~");
                }
            if (socket != null && !socket.isClosed()) {
                System.out.println(ip + "客户端关闭成功！success~");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(ip + "客户端关闭失败");
                }
            }
        }
    }
    //1.start 方法下--封装 客户端 相互通话方法 chat
    //重构：chat 聊天功能方法：提供给判断条件使用
    private void chat(Scanner scanner){
        new Thread(()->{
            try {
                String message;
                while ((message = br.readLine()) != null) { // 读入服务器端
                    System.out.println("服务器端说：" + message);

                    if("exit".equalsIgnoreCase(message)){
                        break;
                    }
                }
            } catch (IOException e) {
                System.out.println("服务器端发来的消息断开了~");
            }
        }).start();
        while (true) {
            System.out.print("客户端请输入(输入exit退出)：");
            String say = scanner.nextLine();
            pw.println("客户端说：" + say);

            if (say.equalsIgnoreCase("exit")) {
                pw.println("exit");
                break;
            }
        }
    }


    public static void main(String[] args) {
        new Client().start();
    }
}
